import {defineStore} from 'pinia'

import {ref,reactive} from "vue";
const modules = import.meta.glob("../views/*/*.vue");


const piniaPluginConfig = {persist: {storage: sessionStorage}}
/**
 * 导航书签tag
 */
export const useRouteTagStore = defineStore('routeTag', () => {
    const navTags = ref([])


    function addRouteTag(tag) {
        navTags.value.push(tag)
    }

    /**
     * 删除书签导航
     * @param tag
     */
    function removeRouteTag(tag) {
        navTags.value = navTags.value.filter(value => value.path !== tag.path)
    }

    function cleanRouteTags() {
        navTags.value = []
    }

    return {navTags, addRouteTag, removeRouteTag, cleanRouteTags}
}, piniaPluginConfig)


export const useLoginStore = defineStore('userLoginStore', () => {
    const loginSetting = reactive({
        loginLoading:false,
        beforeLoginRoutePath:null
    })
    const userInfo = reactive({
        isLogin:false,
        userId:null,
        avatar:null,
        nickName:null,
        email:null,
        roles:[]
    })
    const setLoginUserInfo = (user)=>{
        userInfo.isLogin = true
        userInfo.userId = user.userId
        userInfo.avatar = user.avatar
        userInfo.nickName = user.nickName
        userInfo.email = user.email
        userInfo.roles = user.roles
    }
    const userLogout = ()=>{
        userInfo.isLogin =false
        userInfo.userId = null
        userInfo.avatar = null
        userInfo.nickName = null
        userInfo.email = null
        sessionStorage.removeItem('accessToken')
        sessionStorage.removeItem('refreshToken')
    }
    const userIsLogin = ()=>{
        return userInfo.isLogin
    }
    const getUserLoginInfo = ()=>{
        return userInfo
    }
    const getLoginSetting = ()=>{
        return loginSetting
    }

    const openLoginLoading = ()=>{
        loginSetting.loginLoading = true
    }
    const closeLoginLoading = ()=>{
        loginSetting.loginLoading = false
    }

    const setBeforeLoginRoutePath = (path)=>{
        loginSetting.beforeLoginRoutePath = path
    }
    const getBeforeLoginRoutePath = ()=>{
        return loginSetting.beforeLoginRoutePath
    }
    const getUserRoles = ()=>{
        return userInfo.roles
    }
    return {
        userInfo,
        userIsLogin,setLoginUserInfo,userLogout,getUserLoginInfo,getLoginSetting,closeLoginLoading
        ,openLoginLoading,setBeforeLoginRoutePath,getBeforeLoginRoutePath,getUserRoles
    }
},piniaPluginConfig)


